
export function getTextWidth(str, padding = 60, fontSize = 13) {
    let textCanvas = document.getElementById('textCanvas');
    if (!textCanvas) {
        textCanvas = document.createElement('canvas');
        textCanvas.id = 'textCanvas';
    }
    const ctx = textCanvas.getContext('2d');
    ctx.font = `${fontSize}px  apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"`;
    return ctx.measureText(str).width + padding;
}

export function copyToClipboard(text) {
    if (!text) {
        return
    }
    return new Promise((resolve, reject) => {
        const element = document.createElement('textarea')
        element.value = text
        element.style.position = "fixed"
        document.body.append(element)
        element.select()
        try {
            document.execCommand("copy")
            resolve(true)
        } catch (error) {
            console.log(`内容无法复制到剪贴板: ${error}`)
            resolve(false)
        }
        element.remove()
    })
}
